package com.itheima.prize.commons.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.prize.commons.db.entity.*;
import com.itheima.prize.commons.db.mapper.CardUserHitMapper;
import com.itheima.prize.commons.db.service.CardUserService;
import com.itheima.prize.commons.db.mapper.CardUserMapper;
import com.itheima.prize.commons.db.service.GameLoadService;
import com.itheima.prize.commons.db.service.ViewCardUserHitService;
import com.itheima.prize.commons.utils.ApiResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* @author shawn
* @description 针对表【card_user(会员信息表)】的数据库操作Service实现
* @createDate 2023-12-26 11:58:48
*/
@Service
public class CardUserServiceImpl extends ServiceImpl<CardUserMapper, CardUser>
    implements CardUserService{
    @Autowired
    private CardUserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ViewCardUserHitService hitService;
    @Autowired
    private GameLoadService loadService;
    @Autowired
    private CardUserHitMapper cardUserHitMapper;
    @Override
    public ApiResult login(HttpServletRequest request,String account, String password) {
        ApiResult apiResult=new ApiResult<>(1,null,null);
        if(redisTemplate.opsForValue().get("logincount")==null){
            redisTemplate.opsForValue().set("logincount",0);

        }
        Object count=redisTemplate.opsForValue().get("logincount");
        LambdaQueryWrapper<CardUser> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(CardUser::getUname,account);
        CardUser cardUser = userMapper.selectOne(wrapper);
        if(cardUser==null || !cardUser.getPasswd().equals(password)){
            apiResult.setMsg("用户不存在");
            apiResult.setCode(0);
            int a=Integer.parseInt(count.toString());
            a++;
            redisTemplate.opsForValue().set("logincount",a,5, TimeUnit.SECONDS);
            if(a>5){
                apiResult.setMsg("登录次数过多，请5秒后重试");
            }
            return apiResult;
        }else {
            apiResult.setMsg("成功");
            CardUser user = new CardUser();
            BeanUtils.copyProperties(cardUser, user);
            apiResult.setData(user);
            user.setUpdatetime(new Date());
            redisTemplate.opsForValue().set("logincount",0);
            HttpSession session = request.getSession();
            session.setAttribute( "user", user);
            return apiResult;
        }
    }

    @Override
    public ApiResult getInfo(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if(user==null){
            return new ApiResult<>(-1,"未登录","");
        }
        CardUser user2=(CardUser) user;
        Integer gamesNumByUserId = loadService.getGamesNumByUserId(user2.getId());
        Integer prizesNumByUserId = loadService.getPrizesNumByUserId(user2.getId());
        CardUserDto cardUserDto=new CardUserDto(user2);
        cardUserDto.setGames(gamesNumByUserId);
        cardUserDto.setProducts(prizesNumByUserId);
        ApiResult apiResult=new ApiResult<>(1,"成功",cardUserDto);
        apiResult.setNow(new Date());
        return apiResult;

    }

    @Override
    public ApiResult getPrizeInfo(int gameid, int curpage, int limit, HttpServletRequest request) {
        CardUserHitDto cardUserHitDto=new CardUserHitDto();
        HttpSession session = request.getSession();
        CardUser user=(CardUser) session.getAttribute("user");
        ViewCardUserHit hit=new ViewCardUserHit();
        List<CardUserHit> byUserId = hitService.getByUserId(user.getId());
        Page<ViewCardUserHit> byUserIdlimit = hitService.getByUserIdlimit(curpage, limit, user.getId());
        List<ViewCardUserHit> records = byUserIdlimit.getRecords();
        cardUserHitDto.setCurrentPage(byUserIdlimit.getCurrent());
        cardUserHitDto.setItems(records);
        cardUserHitDto.setPageSize(byUserIdlimit.getSize());
        cardUserHitDto.setTotalNum(byUserIdlimit.getTotal());
        cardUserHitDto.setTotalPage(byUserIdlimit.getPages());
        cardUserHitDto.setStartIndex(byUserIdlimit.getCurrent());
        cardUserHitDto.setIsMore(byUserIdlimit.hasNext());
        ApiResult apiResult=new ApiResult<>(1,"查询成功",cardUserHitDto);

        return apiResult;
    }

    @Override
    public ApiResult logout(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        ApiResult result=new ApiResult<>(1,"","");
        return result;
    }



}




